﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Extjs_ReGSharp.InputParsing;

namespace Extjs_ReGSharp.Criteria.Factory
{
	internal class CriteriaFactory : ICriteriaFactory
	{
		public ICriteria<T> CreateCriteria<T>(CriteriaDefinitionDTO criteriaDefinition)
		{
			FieldTypes currentType = (FieldTypes)Enum.Parse(typeof(FieldTypes), criteriaDefinition.FieldType, true);

			if (currentType == FieldTypes.String)
			{
				return new StartsWithCriteria<T>(criteriaDefinition.FieldName, criteriaDefinition.Value);
			}

			ComparisonTypes comparisonType = (ComparisonTypes)Enum.Parse(typeof(ComparisonTypes), criteriaDefinition.FieldComparison, true);

			if (currentType == FieldTypes.Numeric && comparisonType == ComparisonTypes.gt)
			{
				return new GreaterThanCriteria<T>(criteriaDefinition.FieldName, Int32.Parse(criteriaDefinition.Value));
			}

			if (currentType == FieldTypes.Numeric && comparisonType == ComparisonTypes.lt)
			{
				return new LowerThanCriteria<T>(criteriaDefinition.FieldName, Int32.Parse(criteriaDefinition.Value));
			}

			if (currentType == FieldTypes.Numeric && comparisonType == ComparisonTypes.eq)
			{
				return new EqualsToCriteria<T>(criteriaDefinition.FieldName, Int32.Parse(criteriaDefinition.Value));
			}

			return null;
		}
	}
}
